package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebCorporationUserDao;
import ysq.xyg.web.model.po.WebCorporationUser;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebCorporationUserVo;
import ysq.xyg.web.service.WebCorporationUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-社团人员信息service实现
 *
 * @author shengQiang yu
 * @date 2019-5-13 9:28:08
 */
@Service("webCorporationUserService")
public class WebCorporationUserServiceImpl extends BaseServiceImpl<WebCorporationUser> implements WebCorporationUserService {

    @Resource
    private WebCorporationUserDao webCorporationUserDao;

    private WebCorporationUser webCorporationUser;

    @Autowired
    public void setBaseDao(WebCorporationUserDao webCorporationUserDao) {
        this.baseDao = webCorporationUserDao;
    }


    /**
     * 分页查询
     *
     * @param webCorporationUserVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-13 9:28:08
     */
    public ReturnEntity page(WebCorporationUserVo webCorporationUserVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebCorporationUser> list = acceptList(webCorporationUserVo);
        PageInfo<WebCorporationUser> page=new PageInfo<WebCorporationUser>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webCorporationUserVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-13 9:28:08
     */
    public ReturnEntity addOrUpdate(WebCorporationUserVo webCorporationUserVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webCorporationUserVo.getId())){
            webCorporationUser=new WebCorporationUser(currentUser);
            isUpate=false;
        }else{
            webCorporationUser=selectByPrimaryKey(webCorporationUserVo.getId());
            if(ObjectUtil.isNull(webCorporationUser)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webCorporationUser.setUpdateTime(new Date());
            webCorporationUser.setUpdateUser(currentUser.getId());
        }
        webCorporationUser.setCorporationId(webCorporationUserVo.getCorporationId());
        webCorporationUser.setName(webCorporationUserVo.getName());
        webCorporationUser.setBrithday(webCorporationUserVo.getBrithday());
        webCorporationUser.setWorkPost(webCorporationUserVo.getWorkPost());
        webCorporationUser.setPolitical(webCorporationUserVo.getPolitical());
        webCorporationUser.setUserType(webCorporationUserVo.getUserType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webCorporationUser);
        }else{
            num=insert(webCorporationUser);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-13 9:28:08
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebCorporationUser webCorporationUser=selectByPrimaryKey(id);
        if(webCorporationUser!=null){
            webCorporationUser.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webCorporationUser.setUpdateTime(new Date());
            webCorporationUser.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webCorporationUser);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webCorporationUserVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-13 9:28:08
     */
    public List<WebCorporationUser> acceptList(WebCorporationUserVo webCorporationUserVo){
        Example example = new Example(WebCorporationUser.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webCorporationUserVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webCorporationUserVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webCorporationUserVo.getCorporationId())){
            criteria.andEqualTo("corporationId",webCorporationUserVo.getCorporationId());
        }
        if(StringUtil.isNotEmpty(webCorporationUserVo.getUserType())){
            criteria.andEqualTo("userType",webCorporationUserVo.getUserType());
        }
        if(StringUtil.isNotEmpty(webCorporationUserVo.getName())){
            criteria.andLike("name","%"+webCorporationUserVo.getName()+"%");
        }
        example.setOrderByClause(" update_time desc");
        return webCorporationUserDao.selectByExample(example);
    }
}
